AddModCharacter("musha", "FEMALE")

local Skill_Menu = require("widgets/Skill_Menu")
local function addSkillWidget(self)
	self.musha_skill_menu = self:AddChild(Skill_Menu())
	self.musha_skill_menu:SetHAnchor(2)
	self.musha_skill_menu:SetVAnchor(0)
	self.musha_skill_menu:SetPosition(-600, 0, 0)
end
AddClassPostConstruct("widgets/controls", addSkillWidget)

AddAction(
	"Musha_PowerAttack",
	STRINGS.MUSHA_SKILLS.POWERATTACK,
	function(act)
		if act.doer ~= nil and act.target ~= nil and act.target ~= act.doer and act.doer:HasTag("_musha") then
			act.doer.components.combat:SetAreaDamage(3.5, act.doer.components.musha_skills.skilldata("PowerAttack", "damage"))
			act.doer.components.musha_skills:PowerAttack(act.target)
			act.doer.components.combat:DoAttack(act.target)
			act.doer.components.combat:SetAreaDamage(0, 0)
			return true
		else
			return false
		end
	end
)

AddAction(
	"Musha_ElectricBook",
	STRINGS.MUSHA_SKILLS.LIGHTATTACK,
	function(act)
		if
			act.doer ~= nil and act.target ~= nil and act.doer:HasTag("_musha") and act.target.components.musha_skills and
				act.doer.components.musha_spellpower and
				act.doer.components.musha_spellpower.current > 20
		 then
			act.target.components.musha_skills:Electric_Book(act.doer)
			return true
		else
			return false
		end
	end
)

AddAction(
	"Musha_REPAIR",
	STRINGS.ACTIONS.ADDFUEL,
	function(act)
		if act.target and act.target.components.musha_equipment then
			local material
			if
				act.doer and act.doer.components.inventory and act.doer.components.inventory:IsHeavyLifting() and
					not (act.doer.components.rider and act.doer.components.rider:IsRiding())
			 then
				material = act.doer.components.inventory:GetEquippedItem(EQUIPSLOTS.BODY)
			else
				material = act.invobject
			end
			if material and material.components.musha_fuel then
				act.target.components.musha_equipment:TakeFuelItem(act.doer, material)
				return true
			end
		end
	end
)

AddComponentAction(
	"SCENE",
	"combat",
	function(inst, doer, actions, right)
		if
			right and doer.replica.health and not doer.replica.health:IsDead() and doer.replica.musha_skills and
				doer.replica.musha_skills.skill_point["PowerAttack"]:value() > 0
		 then
			if inst ~= doer and doer.replica.combat:GetWeapon() then
				table.insert(actions, ACTIONS.Musha_PowerAttack)
			end
		end
	end
)

AddComponentAction(
	"SCENE",
	"musha_skills",
	function(inst, doer, actions, right)
		if right and doer.replica.health and not doer.replica.health:IsDead() and doer.replica.musha_skills then
			if inst == doer then
				table.insert(actions, ACTIONS.Musha_ElectricBook)
			end
		end
	end
)

AddComponentAction(
	"USEITEM",
	"musha_fuel",
	function(inst, doer, target, actions, right)
		if right then
			if doer.replica.rider ~= nil and doer.replica.rider:IsRiding() then
				if not (target.replica.inventoryitem ~= nil and target.replica.inventoryitem:IsGrandOwner(doer)) then
					return
				end
			elseif doer.replica.inventory ~= nil and doer.replica.inventory:IsHeavyLifting() then
				return
			end
			if target.components.musha_equipment then
				table.insert(actions, ACTIONS.Musha_REPAIR)
			end
		end
	end
)

ACTIONS.Musha_REPAIR.priority = 3
ACTIONS.Musha_PowerAttack.priority = 2

if TheNet:GetIsServer() then
	local function AddFuelType(fuel_list, fuel_value)
		for k, v in pairs(fuel_list) do
			AddPrefabPostInit(
				v,
				function(inst)
					inst:AddComponent("musha_fuel")
					inst.components.musha_fuel:InitFuel(fuel_value)
				end
			)
		end
	end

	AddFuelType(
		{
			"rocks",
			"flint",
			"nitre",
			"marble",
			"moonrocknugget",
			"thulecite_pieces",
			"boneshard",
			"stinger",
			"spidergland",
			"houndstooth",
			"snakeskin",
			"slurtle_shellpieces",
			"silk"
		},
		50
	)
	AddFuelType(
		{
			"goldnugget",
			"thulecite"
		},
		100
	)

	AddPlayerPostInit(
		function(inst)
			local function Resurrect(inst)
				local x, y, z = inst.Transform:GetWorldPosition()
				local ents = TheSim:FindEntities(x, y, z, 20, {"yamche_green"})
				for k, v in pairs(ents) do
					if v.components.container and v.components.container:Has("musha_cristal") then
						v.components.container:ConsumeByName("musha_cristal", 1)
						inst.components.health.numrevives = 0
						return
					end
				end
			end
			inst:ListenForEvent("death", Resurrect)
			inst:ListenForEvent("ms_becameghost", Resurrect)
		end
	)

	AddClassPostConstruct(
		"components/health",
		function(self, inst)
			inst:DoTaskInTime(
				0,
				function(inst)
					inst:AddComponent("musha_exp_assigner")
				end
			)
		end
	)
else
end
